﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Sale
{
    public partial class Sale_MactivityChange : CommonPageBase
    {
        List<TempFile> files = new List<TempFile>();
        public string ContentS = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DataBaseHelper.BindDrp(Drp_STATUS, EnumData.DataBaseEunm.MactivitySTATUS);
                FormInfo.ShowTriggerWin(Window1, txt_CONTAT, hid_CONTAT, "/PublicModel/SelectUsers.aspx", "选择申请人");

                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                LoadData();
                BindGrid();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {

                Sale_MACTIVITY Sam = TbsDbSession.Default.From<Sale_MACTIVITY>().Where(Sale_MACTIVITY._.MVID == int.Parse(Request["id"])).ToFirst();
                if (Sam != null)
                {
                    txt_ASUBJECT.Text = Sam.ASUBJECT;
                    txt_BADGET.Text = Sam.BADGET;
                    txt_CONTAT.Text = Sam.CONTAT;
                    txt_COTELNUB.Text = Sam.COTELNUB;
                    txt_EDATE.Text = Sam.EDATE.Value.ToString("yyyy-MM-dd");
                    txt_RECORD.Text = Sam.RECORD;
                    txt_REMARK.Text = Sam.REMARK;
                    txt_SDATE.Text = Sam.SDATE.Value.ToString("yyyy-MM-dd") ;
                    txt_SPEND.Text = Sam.SPEND;
                    txt_SUMMARY.Text = Sam.SUMMARY;
                    txt_ADDRES.Text = Sam.ADDRES;
                    txt_DESCRIBE.Text = Sam.DESCRIBE;
                    hf.Text = Sam.ANNEX;
                }
            }
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                String[] str = { "1", "2", "3", "4", "5", "6", "7" };
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
                hf.Text += fileName + ";";
                // 清空文件上传组件
                filePhoto.Reset();
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            List<string> ls = StringHelper.GetSubStringList(hf.Text, ';');
            foreach (string item in ls)
            {
                TempFile tf = new TempFile();
                tf.FileName = item;
                files.Add(tf);
            }
            Grid1.DataSource = files;
            Grid1.DataBind();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string[] str = Grid1.Rows[e.RowIndex].Values;
                hf.Text = hf.Text.Replace(str[1], "");
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[0] = "<a href=\"/upload/" + e.Values[0] + "\">" + e.Values[0] + "</a>";
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Sale_MACTIVITY Sam = new Sale_MACTIVITY();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Sam.MVID = int.Parse(Request["id"]);
                Sam.Attach();
                Sam.ADDRES = txt_ADDRES.Text;
                Sam.ANNEX = hf.Text;
                Sam.ASUBJECT = txt_ASUBJECT.Text;
                Sam.BADGET = txt_BADGET.Text;
                Sam.CONTAT = txt_CONTAT.Text;
                Sam.COTELNUB = txt_COTELNUB.Text;
                Sam.DESCRIBE = txt_DESCRIBE.Text;
                Sam.EDATE = Convert.ToDateTime(txt_EDATE.Text);
                Sam.RECORD = txt_RECORD.Text;
                Sam.REMARK = txt_REMARK.Text;
                Sam.SDATE = Convert.ToDateTime(txt_SDATE.Text);
                Sam.SPEND = txt_SPEND.Text;
                Sam.STATUS = Drp_STATUS.SelectedValue;
                Sam.SUMMARY = txt_SUMMARY.Text;
                returnvalue = TbsDbSession.Default.Update<Sale_MACTIVITY>(Sam);
            }
            else
            {
                Sam.DeAttach();
                Sam.ADDRES = txt_ADDRES.Text;
                Sam.ANNEX = hf.Text;
                Sam.ASUBJECT = txt_ASUBJECT.Text;
                Sam.BADGET = txt_BADGET.Text;
                Sam.CONTAT = txt_CONTAT.Text;
                Sam.COTELNUB = txt_COTELNUB.Text;
                Sam.DESCRIBE = txt_DESCRIBE.Text;
                Sam.EDATE = Convert.ToDateTime(txt_EDATE.Text);
                Sam.RECORD = txt_RECORD.Text;
                Sam.REMARK = txt_REMARK.Text;
                Sam.SDATE = Convert.ToDateTime(txt_SDATE.Text);
                Sam.SPEND = txt_SPEND.Text;
                Sam.STATUS = Drp_STATUS.SelectedValue;
                Sam.SUMMARY = txt_SUMMARY.Text;
                returnvalue = TbsDbSession.Default.Insert<Sale_MACTIVITY>(Sam);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }



    }
}